//
//  EnumVC.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/16.
//  Copyright © 2017年 毕小强. All rights reserved.
//

import UIKit

class EnumVC: UIViewController {
    
    /// 隐式原始值
    ///
    /// - north: "north"
    /// - south: "south"
    /// - west: "west"
    /// - east: "east"
    enum CompassPoint:String {
        case north
        case south
        case west
        case east
    }
    
    
    /// 当操作存储整数或者字符串的时候,不必显式的给每个成员非配原始值
    /// 隐式置顶原始值
    /// - mercury:
    /// - venus:
    /// - earth:
    /// - mars:
    /// - jupiter:
    enum Planet:Int {
        case mercury = 1,venus,earth,mars,jupiter
    }
    
    enum Barcode {
        case upc(Int,Int,Int,Int)
        case qrCode(String)
    }
    
    
    /// 原始值
    ///
    /// - tab: 制表符
    /// - lineFeed: 换行符
    /// - carriageReturn: 回车
    /// - space: 空格
    enum ASCIIControlCharacter:Character {
        case tab = "\t"
        case lineFeed = "\n"
        case carriageReturn = "\r"
        case space = " "
    }
    
    /// 递归枚举
    ///
    /// - number: 枚举值
    indirect enum ArithmeticExpression {
        case number(Int)
        case addition(ArithmeticExpression,ArithmeticExpression)
        case multiplication(ArithmeticExpression,ArithmeticExpression)
        case subtraction(ArithmeticExpression,ArithmeticExpression)
        case division(ArithmeticExpression,ArithmeticExpression)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        var directionToHead = CompassPoint.east
        directionToHead = .west
        switch directionToHead {
        case .east:
            print("东")
        case .west:
            print("西")
        case .north:
            print("北")
        case .south:
            print("南")
        }
        
        var productBarcode = Barcode.upc(8, 85909, 51226, 3)
        switch productBarcode {
        case let .upc(a, b, c, d):
            print("upc:\(a) \(b) \(c) \(d)")
        default:
            print("Nothing")
        }
        
        productBarcode = .qrCode("ABCDEFG")
        switch productBarcode {
        case let .qrCode(str):
            print("qrCode:\(str)")
        default:
            print("Nothing")
        }
        
        //通过rawValue访问原始值
        print(CompassPoint.east.rawValue)
        
        //从原始值初始化
        let possiblePlanet = Planet(rawValue: 6)
        print(possiblePlanet?.rawValue ?? "并没有")
        
        let five = ArithmeticExpression.number(5)
        let four = ArithmeticExpression.number(4)
        let sum = ArithmeticExpression.addition(five, four)
        let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
        print(evaluate(product))
        
        
        // Do any additional setup after loading the view.
    }
    
    /// 利用枚举实行加减乘除
    ///
    /// - Parameter expression: 形参
    /// - Returns: 结果
    func evaluate(_ expression:ArithmeticExpression) -> Int {
        switch expression {
        case let .number(value):
            return value
        case let .addition(left,right):
            return evaluate(left) + evaluate(right)
        case let .multiplication(left, right):
            return evaluate(left)*evaluate(right)
        case let .subtraction(left, right):
            return evaluate(left)-evaluate(right)
        case let .division(left, right):
            return evaluate(left)/evaluate(right)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
